package com.comtop.springboot.templet.modules.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.comtop.springboot.templet.modules.sys.mapper.SysRoleMenuMapper;
import com.comtop.springboot.templet.modules.sys.service.ISysRoleMenuService;
import com.comtop.springboot.templet.modules.sys.entity.SysRoleMenuEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



/**
 * 角色与菜单对应关系
 * 
 * @author wangjiafang
 * @version 1.0
 * @since 2016年9月18日 上午9:44:35
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper,SysRoleMenuEntity> implements ISysRoleMenuService {
	/**角色-菜单关系服务**/
	@Autowired
	private SysRoleMenuMapper sysRoleMenuMapper;

	@Override
	@Transactional
	public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
		//先删除角色与菜单关系
		//sysRoleMenuMapper.delete(roleId);
		sysRoleMenuMapper.delete(new EntityWrapper<SysRoleMenuEntity>().eq("role_id",roleId));

		if(menuIdList.size() == 0){
			return ;
		}

		//保存角色与菜单关系
		List<SysRoleMenuEntity> sysRoleMenuEntities = new ArrayList<>(menuIdList.size());
		SysRoleMenuEntity sysRoleMenuEntity=null;
		for (long menuId:menuIdList){
			sysRoleMenuEntity = new SysRoleMenuEntity();
			sysRoleMenuEntity.setRoleId(roleId);
			sysRoleMenuEntity.setMenuId(menuId);
			sysRoleMenuEntities.add(sysRoleMenuEntity);
		}
		insertBatch(sysRoleMenuEntities);

		//sysRoleMenuMapper.save(map);
	}

	@Override
	public List<Long> queryMenuIdList(Long roleId) {
		return sysRoleMenuMapper.queryMenuIdList(roleId);
	}

}
